package com.laf.manager.controller;

import com.laf.manager.controller.dto.PointsDeductionDto;
import com.laf.manager.dto.Industry;
import com.laf.manager.dto.PlaneMap;
import com.laf.manager.dto.Shop;
import com.laf.manager.querycondition.DeductionQueryCondition;
import com.laf.manager.querycondition.shop.ShopQueryCondition;
import com.laf.manager.service.PointsDeductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ygl
 * @create 2019-01-03
 * @DESCRIPTION
 **/
@Controller
@Slf4j
public class PointsDeductionController {

    @Autowired
    private PointsDeductionService pointsDeductionService;


    @GetMapping("/pointsDeduction")
    public ModelAndView getShopsForPost(HttpSession session) {
        DeductionQueryCondition condition = null;

        if (session.getAttribute("deduction_queryCondition") != null) {
            condition = (DeductionQueryCondition) session.getAttribute("deduction_queryCondition");
        } else {
            condition = new DeductionQueryCondition();
        }

        List<PointsDeductionDto> list = pointsDeductionService.getList(condition);

        int total = pointsDeductionService.getCountList(condition);

        ModelAndView model = new ModelAndView();

        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("index", condition.getPage());
        pageMap.put("size", condition.getSize());
        pageMap.put("total", total);

        model.getModel().put("pageMap", pageMap);
        model.getModel().put("list", list);
        model.setViewName("pointsDeduction");

        return model;
    }

    @PostMapping("/pointsDeduction/filter")
    public String filterActivities(DeductionQueryCondition condition, BindingResult errors, HttpSession session) {
        session.setAttribute("deduction_queryCondition", condition);

        return "redirect:/pointsDeduction";
    }


}
